package com.ruoyi.file.controller;

import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.file.domain.SysOssConfig;
import com.ruoyi.file.domain.bo.SysOssConfigBo;
import com.ruoyi.file.domain.vo.SysOssConfigVo;
import com.ruoyi.file.service.ISysOssConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 对象存储配置
 *
 * @author Lion Li
 * @author 孤舟烟雨
 * @date 2021-08-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/oss/config")
public class SysOssConfigController extends BaseController {

	private final ISysOssConfigService iSysOssConfigService;

	/**
	 * 查询对象存储配置列表
	 */
	@GetMapping("/list")
	public TableDataInfo<SysOssConfigVo> list(@Validated(QueryGroup.class) SysOssConfigBo bo, PageQuery pageQuery) {
		return iSysOssConfigService.queryPageList(bo, pageQuery);
	}

	/**
	 * 获取对象存储配置详细信息
	 *
	 * @param ossConfigId OSS配置ID
	 */
	@GetMapping("/{ossConfigId}")
	public R<SysOssConfigVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long ossConfigId) {
		return R.ok(iSysOssConfigService.queryById(ossConfigId));
	}

	/**
	 * 新增对象存储配置
	 */
	@RepeatSubmit()
	@PostMapping()
	public R<Void> add(@Validated(AddGroup.class) @RequestBody SysOssConfigBo bo) {
		return toAjax(iSysOssConfigService.insertByBo(bo));
	}

	/**
	 * 修改对象存储配置
	 */
	@RepeatSubmit()
	@PutMapping()
	public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysOssConfigBo bo) {
		return toAjax(iSysOssConfigService.updateByBo(bo));
	}

	/**
	 * 删除对象存储配置
	 *
	 * @param ossConfigIds OSS配置ID串
	 */
	@DeleteMapping("/{ossConfigIds}")
	public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ossConfigIds) {
		return toAjax(iSysOssConfigService.deleteWithValidByIds(Arrays.asList(ossConfigIds), true));
	}

	/**
	 * 状态修改
	 */
	@PutMapping("/changeStatus")
	public R<Void> changeStatus(@RequestBody SysOssConfigBo bo) {
		return toAjax(iSysOssConfigService.updateOssConfigStatus(bo));
	}

	@GetMapping("/getDefaultConfig")
	public R<SysOssConfig> getDefaultConfig() {
		SysOssConfig config =  iSysOssConfigService.getDefaultConfig();
		return R.ok(config);
	}
}
